package com.zzptc.sky.baidudemo.utils;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.socks.library.KLog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zzptc.sky.baidudemo.BuildConfig;
import com.zzptc.sky.baidudemo.Constants;
import com.zzptc.sky.baidudemo.R;
import com.zzptc.sky.baidudemo.db.BaiBoxInfoDao;
import com.zzptc.sky.baidudemo.db.DaoMaster;
import com.zzptc.sky.baidudemo.db.DaoSession;
import com.zzptc.sky.baidudemo.vo.BaiBoxInfo;
import com.zzptc.sky.baidudemo.vo.ContactsInfo;

import org.greenrobot.greendao.database.Database;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * Created by Administrator on 2017/4/20.
 */

public class MyApplication extends Application {
    private static Context context;
    private static DaoSession BBXdaoSession;
    private static DaoSession MobiledaoSession;
    private static List<ContactsInfo> contactsList;

    public static List<ContactsInfo> getContactsList() {
        return contactsList;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        context = getApplicationContext();

        KLog.init(BuildConfig.LOG_DEBUG, Constants.LOG_FLAG);

        initClient();

        initDataBase();

        new Thread(new Runnable() {
            @Override
            public void run() {
                initDataToTable();
            }
        }).start();

        //开线程 复制外部数据库  初始化数据库的GreenDao
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    MyUntils.copyFileToMobile(context,"mobile.db");
                    initData();
                    contactsList = MyUntils.getAllContacts();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //返回上下文
    public static Context getContext(){
        return context;
    }

    public void initClient(){
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new LoggerInterceptor("TAG"))
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                //其他配置
                .build();
        OkHttpUtils.initClient(okHttpClient);
    }

    //初始化百宝箱的GreenDao
    private static void initDataBase(){
        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(context,Constants.DATABASE_NAME);
        Database database = openHelper.getWritableDb();
        DaoMaster daoMaster = new DaoMaster(database);
        BBXdaoSession = daoMaster.newSession();
    }

    //返回百宝箱的daoSession
    public static DaoSession getBoxSession(){
        return BBXdaoSession;
    }

    //将数据新增到数据库。 只操作一次
    public void initDataToTable(){
        DaoSession daoSession = getBoxSession();
        BaiBoxInfoDao dao = daoSession.getBaiBoxInfoDao();

        SharedPreferences sp = MySharedPreferences.getSharedPreferences();
        boolean b = sp.getBoolean("firstInsertTable",false);

        if (!b){
            String[] names = getResources().getStringArray(R.array.bbx_name);
            String[] icons = getResources().getStringArray(R.array.bbx_icon);

            for (int i = 0;i<names.length;i++){
                BaiBoxInfo boxInfo = new BaiBoxInfo(null,names[i],icons[i]);
                long l = dao.insert(boxInfo);
                if (l>0){
                    Log.d("11","新增成功");
                }else {
                    Log.d("11","新增失败");
                }
            }

            SharedPreferences.Editor editor = sp.edit();
            editor.putBoolean("firstInsertTable",true);
            editor.commit();
        }
    }

    //初始化MobileInfo 的GreenDao
    private static void initData(){
        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(context,"mobile.db");
        Database database = openHelper.getReadableDb();
        DaoMaster daoMaster = new DaoMaster(database);
        MobiledaoSession = daoMaster.newSession();
    }

    //返回MobileInfo的daoSession
    public static DaoSession getMobileSession(){
        return MobiledaoSession;
    }
}
